<template>
  <div>
    <!-- 弹窗组件 -->
    <el-dialog :title="formData.id?'编辑经纪':'新增经纪'" :visible="isShowDialog" @close="btnCancel">
      <el-form ref="addForm" :model="formData" :rules="rules">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="formData.email" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="formData.address" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="formData.desc" />
        </el-form-item>
        <el-form-item label="封面">
          <!-- 放置上传图片组件 -->
          <ImageUpload ref="formDataImage" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center" class="operation">
            <el-button type="primary" size="small" @click="btnOk">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import ImageUpload from './ImageUpload/index.vue'
import { addAngent, editAngent, getAngentDetail } from '@/api/angent'
export default {
  components: { ImageUpload },
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '电话不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '地址', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      },
      userid: ''
    }
  },
  methods: {
    async btnOk() {
      // 校验
      await this.$refs.addForm.validate()

      const fileList = this.$refs.formDataImage.fileList
      console.log('fileList', fileList)
      this.formData.image = fileList[0].url

      console.log(84, this.formData)
      // 发请求
      if (this.formData.id) {
        await editAngent(this.formData)
      } else {
        await addAngent(this.formData)
      }

      // 提示用户
      this.$message.success('操作成功')

      // 刷新页面
      // this.$emit('addAngent')
      this.$parent.getAngetCount()
      this.$parent.getAngentList()

      // 关闭弹窗
      this.$emit('update:isShowDialog', false)
    },
    btnCancel() {
      // 清空表单
      this.formData = {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }
      // 清空报错
      this.$refs.addForm.resetFields()

      // 关闭弹窗
      this.$emit('update:isShowDialog', false)
      this.$refs.formDataImage.fileList = []
    },
    // 回显
    async getAngentDetail(id) {
      this.userid = id
      const { data } = await getAngentDetail(id)
      console.log(108, data)
      this.formData = data
      console.log(115, this.formData)
      this.$refs.formDataImage.fileList = [
        {
          url: this.formData.image
        }
      ]
    }
  }

}
</script>

<style>

</style>
